/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 8/19/11
 * Time: 5:53 PM
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;

public class TaskA extends Thread {
    public TaskA() {
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.output = new PrintWriter(System.out);
        this.setPriority(Thread.MAX_PRIORITY);
    }
    static String cards = "6789TJQKA";

    private void solve() throws Throwable {
        String god = nextToken();
        String first = nextToken(), second = nextToken();
        boolean can = false;
        if (first.charAt(1) != second.charAt(1)) {
            if (first.charAt(1) == god.charAt(0)) {
                can = true;
            } else {
                can = false;
            }
        } else {
            if (cards.indexOf(first.charAt(0)) > cards.indexOf(second.charAt(0))) {
                can = true;
            }
        }
        output.println(can ? "YES" : "NO");
    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }

    public static void main(String[] args) {
        new TaskA().start();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}
